// pages/getloginpaw/getloginpaw.js
import WxValidate from '../../utils/WxValidate.js';
var app=getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    buttonText:'获取验证码',
    oldPhoen: '',
    tel: ''
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.initValidate();
    let self = this;
    wx.request({
      url: app.globalData.reqPath + 'shop/Members',
      data: {},
      header: {
        'authorization': app.globalData.authorization
      },
      success: function ({ data, header }) {
        //console.log("个人信息：" + data)
        if (data.code == 200) {
          self.setData({ oldPhoen: data.data.phone })
        } else {
          wx.showToast({
            title: data.data.msg,
            icon: 'none'
          })
        }
      }
    })
    wx.hideShareMenu()
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },
  //表单验证
  initValidate() {
    const rules = {
      
      vericode: {
        required: true,
        number: true,
        maxlength: 8
      },
      newpaw:{
        required: true
      },
    }
    const messages = {
      
      vericode: {
        required: "请输入验证码",
        number: "请输入正确的验证码",
        maxlength: '请输入正确的验证码'
      },
      newpaw: {
        required: '请输入新登录密码',
      }
    }
    this.WxValidate = new WxValidate(rules, messages)
  },
  showModal: function (error) {
    wx.showToast({
      title: error.msg,
      icon: 'none'
    })
  },
  //获取验证码
  getCode: function () {
    let phone = this.data.oldPhoen;
    let self = this;
    // if (phone.match(/^(13[0-9]|14[579]|15[0-3,5-9]|16[6]|17[0135678]|18[0-9]|19[89])\d{8}$/)) {
      wx.request({
        url: app.globalData.reqPath + 'shop/MembersSms',
        header: {
          'authorization': app.globalData.authorization
        },
        data: {
          phone: self.data.tel
        },
        method: "POST",
        success: function ({ data, header }) {
          // console.log(res.data);
          app.globalData.authorization = header.authorization
          let num = 61;
          var timer = setInterval(function () {
            num--;
            if (num <= 0) {
              clearInterval(timer);
              self.setData({
                buttonText: '重新发送',
                disabled: false
              })
            } else {
              self.setData({
                buttonText: num + "s后重新发送",
                disabled: true
              })
            }
          }, 1000)
        }

      })
    // } else {
    //   wx.showToast({
    //     title: '请输入正确的手机号',
    //     icon: 'none'
    //   })
    // }
  },
  subreset:function(e){
    let parames = e.detail.value
    if (!this.WxValidate.checkForm(parames)) {
      const error = this.WxValidate.errorList[0]
      this.showModal(error)
      return false;
    }
    let data={
      password:parames.newpaw,
      code: parames.vericode,
      phone: this.data.oldPhoen
    }
    wx.request({
      url: app.globalData.reqPath +'Members/password',
      header:{
        'authorization': app.globalData.authorization
      },
      method:'POST',
      data:data,
      success:function({data}){
        if(data.code==200){
          wx.showToast({
            title: '修改成功',
            success:function(){
              wx.reLaunch({
                url: '/pages/login/login',
              })
            }
          })
        }
      }
    })
  }
})